﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.DAL;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.Model;

namespace DispatchAssist.BusinessLogical.BLL
{
    public class MessageCreateManger
    {

        private static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        private IDataProvider _provider;

        public MessageCreateManger()
        {
            _provider = new SqlDataProvider();
        }

        #region Create AcarsMessage
        public List<AlertAllInfo> GetNecessaryAlertInfo(Enums.AlertActionFlag selectFlag, Enums.AlertActionFlag updateFlag)
        {
            return _provider.GetNecessaryAlertInfo(Guid.Empty, (int)selectFlag, (int)updateFlag);
        }

        public MsgTemplate GetMsgTemplate(string ID, int AlertAction, string TargetType)
        {
            return _provider.GetMsgTemplate(ID, AlertAction, TargetType)[0];
        }

        public ACARSDevice GetDeviceModel(string tailNo)
        {
            List<ACARSDevice> list = _provider.GetDeviceModel(tailNo);

            if (list == null || list.Count == 0)
            {
                throw new Exception("get Acars type error,TailNo is" + tailNo);
            }

            return list[0];
        }

        public ACARSMsg InsertACARSMsgModel(ACARSMsg model)
        {
            return _provider.InsertACARSMsgModel(model);
        }

        public void InsertACARSMsgModelList(List<ACARSMsg> list)
        {

            foreach (ACARSMsg msg in list)
            {
                InsertACARSMsgModel(msg);
            }

        }

        public void InsertAcarsMsgModelAndUpdateStatus(ACARSMsg model)
        {
            try
            {
                InsertACARSMsgModel(model);

                string[] ids = { model.AlertID.ToString() };

                UpdateAlertStatus(ids, (int)Enums.AlertActionFlag.ManualSendAcrarsAlert);
            }
            catch (Exception e)
            {
                Logger.Error(string.Format(" Insert and UPdate error, AlertID: {0}", model.AlertID), e);
            }
        }


        public void UpdateAlertStatus(List<AlertAllInfo> list)
        {
            if (list.Count > 0)
            {
                var group = list.GroupBy(f => f.PerformActionFlag);

                foreach (var items in group)
                {
                    int status = items.Key;

                    string[] ids = items.ToDictionary<AlertAllInfo, string>(f => f.AlertID.ToString()).Keys.ToArray();

                    //_provider.UpdateAlertStatus(ids, status);

                    UpdateAlertStatus(ids, status);
                }
            }
        }

        public void UpdateAlertStatus(string[] ids, int status)
        {
            try
            {
                _provider.UpdateAlertStatus(ids, status);
            }
            catch (Exception e)
            {
                Logger.Error("error in Update status", e);
            }
        }

        public List<ACARSMsg> GetUnSendAcarsMsg()
        {

            return _provider.GetSendAcarsMsgList();
        }


        public void UpdateAcarsMsgStatus(string[] Ids,int Status)
        {
            _provider.UpdateAcarsMsgStatus(Ids, Status);
        }

        public void UpdateAcarsMsgStatus(List<ACARSMsg> msgList)
        {
            var groups = msgList.GroupBy(f => f.MsgStatus);

            foreach (var items in groups)
            {
                int status = items.Key;

                string[] ids = items.ToDictionary<ACARSMsg, string>(f => f.ACARSMsgID.ToString()).Keys.ToArray();

                UpdateAcarsMsgStatus(ids, status);
            }
        }



        #endregion

        #region Create SMSMessage

        public List<SMSAlertAllInfo> GetNecessarySMSAlert(Enums.AlertActionFlag selectFlag, Enums.AlertActionFlag performFlag)
        {

            return _provider.GetNecessarySMSAlert(Guid.Empty, selectFlag, performFlag);
        }

        public void InertSMSMessageModel(SMSMsg model)
        {
            _provider.InertSMSMessageModel(model);
        }

        public void InsertSMSModelAndUpdateStatus(SMSMsg model)
        {
            try
            {
                InertSMSMessageModel(model);

                string[] ids = { model.SMSAlertID.ToString() };

               // UpdateSMSAlertStatus(ids, Enums.AlertActionFlag.ManualSendSMSAlert);
            }
            catch (Exception e)
            {
                Logger.Error(string.Format(" Insert and UPdate error, AlertID: {0}", model.SMSAlertID), e);
            }
        }

        public void UpdateSMSAlertStatus(string[] IDs, Enums.AlertActionFlag performAction)
        {
            _provider.UpdateSMSAlertStatus(IDs, performAction);
        }

        public List<SMSContact> GetContactList(string companyCode)
        {
            return _provider.GetContactList(companyCode);
        }

        public List<SMSMsg> GetUnSendSMSMsgList()
        {
            return _provider.GetSendSMSMessageList();
        }

        public void UpdateSMSMsgStatus(List<SMSMsg> msgList)
        {
            var groups = msgList.GroupBy(f => f.MsgStatus);

            foreach (var items in groups)
            {
                int status = items.Key;

                string[] ids= items.ToDictionary<SMSMsg, string>(f => f.SMSMsgID.ToString()).Keys.ToArray();

                UpdateSMSMsgStatus(ids, status);
            }
        }

        public void UpdateSMSMsgStatus(string[] ids,int status)
        {
            _provider.UpdateSMSMsgStatus(ids, status);
        }



        #endregion



    }
}
